﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace MyAuthCore.Tools
{
    public class TokenHelper
    {
        public static string keywords = "1qaz!qaz";
        /// <summary>
        /// 创建一个Token值
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public static string CreateToken(string userName)
        {
            string token = userName + ":" + IDHelper.GetSysUUID();
            return DesHelper.DesEncrypt(token, keywords);
        }
        /// <summary>
        /// 根据token值获得用户名
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static string GetNameByToken(string token)
        {
            try
            {
                string temp = DesHelper.DesDecrypt(token, keywords);
                int index = temp.IndexOf(':');
                if (string.IsNullOrEmpty(temp) || index <= 0)
                {
                    return string.Empty;
                }
                return temp.Substring(0, temp.IndexOf(':'));
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
    }
}
